<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/17
 * Time: 18:35
 */

namespace app\mobilewx\controller;;


use app\Model\UsersModel;
use think\Controller;
//系统
class System extends Controller
{
    /*
     * 修改用户个人资料
     * 信息修改
     */
    public function setting()
    {
        //用户资料
       $data['user']= $getUsers=UsersModel::find(session('user_wx.user_id'));
        return view('mobile@system/setting',$data);
    }
    /*
     * 资料修改
     */
    public function setname()
    {
        $class = new Classify();
        $class->emplogin();//  判断是否登陆
        $getData = input('post.');
        $code=['nickname'=>'昵称',

                'mobile'=>'手机号码',
                'mobile_code'=>'手机验证码',
                'old_password'=>'原密码',
                'new_password'=>'新密码',
                'confirm_password'=>'确认密码',
            ];
        foreach ($getData as $k=>$v){
            if ($k=='email'){
                break;
            }
            if($v==''){
              return  retJson('',201,$code[$k].'不能为空');
            }
        }

        if ('name'==$getData['code']){//姓名等信息
//            $this->setnick($getData);
            $a=1;
        }elseif ('mobile'==$getData['code']){//手机号
//            dump($getData);die();
            $where=['mobile'=>$getData['mobile'],
                    'is_del'=>0];
            $getUser=UsersModel::where($where)->find();//手机是否存在
            if ($getUser){return retJson('','201','该手机已经注册');}
            //验证码

            $code=session($getData['mobile'])['code'];
            if ((string)$code!=$getData['mobile_code']){
                return retJson('','201','验证码错误');
            }
            $getData['nickname']=$getData['mobile'];
        }elseif ('pass'==$getData['code']){//密码
            if ($getData['confirm_password']!=$getData['new_password']){
              return  retJson('',201,'确认密码不一致');
            }

            $encpass = encryption($getData['old_password']);
            if ($encpass!=session('user_wx.password')){
              return  retJson('',201,'原密码错误');
            }
            $getData['password']= encryption($getData['confirm_password']);
        }
        //头像
        $files = request()->file('image');
        if ($files){
            foreach($files as $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->validate(['size' => 3145728, 'ext' => 'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'upload/head');
                if ($info) {
                    // 成功上传后 获取上传信息
                    $names = str_replace("\\", "/", $info->getSaveName());//把反斜杠(\)替换成斜杠(/)
                    $url= '/upload/head/'.$names;//图片地址

                } else {
                    // 上传失败获取错误信息
                    return  retJson('',201,$file->getError());
//                echo $file->getError();
                }
            }
            $getData['head_pic']=$url;
        }


        unset($getData['code']);
        unset($getData['mobile_code']);
        unset($getData['old_password']);
        unset($getData['new_password']);
        unset($getData['confirm_password']);
        $getData['user_id']=session('user_wx.user_id');
        $setUser = UsersModel::update($getData);
        $getUser = UsersModel::find(session('user_wx.user_id'));
        session('user_wx',$getUser);
        return setDatabase($setUser,'信息修改');
    }
    

}